<template>
    <div class="box">
        <h2>Sign in / Sign up</h2>
        <div class="container" id="container">
            <div class="from-container sign-up-container">
                <form action="">
                    <h1>Create Account</h1>
                    <div class="social-container">
                        <a href="#"><i class="fa fa-facebook" aria-hidden="true"></i></a>
                        <a href="#"><i class="fa fa-google-plus" aria-hidden="true"></i></a>
                        <a href="#"><i class="fa fa-linkedin" aria-hidden="true"></i></a>
                    </div>
                    <span>or use your email for registration</span>
                    <input type="text" placeholder="Name" v-model="registerUser">
                    <input type="email" placeholder="Email" v-model="registrationEmail">
                    <input type="password" placeholder="Password" v-model="registrationPassword">
                    <button @click="Register($event)">Sign up</button>
                </form>
            </div>
            <div class="from-container sign-in-container">
                <form action="">
                    <h1>Sign in</h1>
                    <div class="social-container">
                        <a href="#"><i class="fa fa-facebook" aria-hidden="true"></i></a>
                        <a href="#"><i class="fa fa-google-plus" aria-hidden="true"></i></a>
                        <a href="#"><i class="fa fa-linkedin" aria-hidden="true"></i></a>
                    </div>
                    <span>or use your account</span>
                    <input type="email" placeholder="Email" v-model="Email">
                    <input type="password" placeholder="Password" v-model="Password">
                    <a href="#">Forgot your password?</a>
                    <button @click="ck('homepage', $event)" plain>Sign In</button>
                </form>
            </div>
            <div class="overlay-container">
                <div class="overlay">
                    <div class="overlay-panel overlay-left">
                        <h1>Welcome Back!</h1>
                        <p>
                            To keep connected with us please login with your personal info
                        </p>
                        <button class="ghost" id="signIn" @click="signIn">Sign In</button>
                    </div>
                    <div class="overlay-panel overlay-right">
                        <h1>Hello,Friend!</h1>
                        <p>Enter your personal details and start journey with us</p>
                        <button class="ghost" id="signUp" @click="signUp">Sign Up</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ElNotification } from 'element-plus'
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from "vue-router";
import { useCounterStore } from '../pinia/Pinia'
const store = useCounterStore()
const router = useRouter()
// 路由跳转
let Email = ref('')
let Password = ref('')
// 姓名
let registerUser = ref("");
// 邮箱
let registrationEmail = ref("");
// 密码
let registrationPassword = ref("");
let user = reactive({ MailName: "", AccountNumber: "", Password: "" });
// 登录
function ck(name, event) {
    let User = JSON.parse(localStorage.getItem('Data'))
    console.log(User);
    if (Email.value == User.MailName && Password.value == User.Password) {
        // 本地储存用户信息
        localStorage.setItem("用户名", Email);
        localStorage.setItem("密码", Password);
        store.modifyName(User.AccountNumber)
        ElNotification({
            title: 'Success',
            message: '登录成功',
            type: 'success',
        })
        router.push({ path: `/${name}`, query: { id: "Eason chan" } })
    }
    else if (Email.value == '') {
        ElNotification({
            title: '登录',
            message: '请先输入邮箱地址',
            type: 'warning',
        })
    }
    else if (Email.value !== User.MailName) {
        ElNotification({
            title: '登录',
            message: '邮箱地址不正确',
            type: 'warning',
        })
    } else if (Password.value == '') {
        ElNotification({
            title: '登录',
            message: '请先输入密码',
            type: 'warning',
        })
    }
    else if (Password.value !== User.Password) {
        ElNotification({
            title: '登录',
            message: '密码不正确',
            type: 'warning',
        })
    }
    // 阻止表单默认行为
    event.preventDefault();
}
// 注册
function Register(event) {
    // 正则表达判断
    // 判断邮箱格式
    let aqq = /^[1-9][0-9]{4,10}@qq.com$/
    //判断密码格式
    let pass = /^[0-9]{6,18}$/
    if (registerUser.value != "") {
        if (aqq.test(registrationEmail.value)) {
            if (pass.test(registrationPassword.value)) {
                user.MailName = registrationEmail.value;
                user.AccountNumber = registerUser.value;
                user.Password = registrationPassword.value;
                localStorage.setItem("Data", JSON.stringify(user));
                ElNotification({
                    title: "注册",
                    message: "注册成功,快去登录吧!",
                    type: "success",
                });
                registerUser.value = "";
                registrationEmail.value = "";
                registrationPassword.value = "";
                container.classList.remove("right-panel-active");
            } else if(registrationPassword.value == ""){
                ElNotification({
                    title: "注册",
                    message: "未输入密码",
                    type: "warning",
                });
            }else{
                ElNotification({
                    title: "注册",
                    message: "密码格式不正确",
                    type: "warning",
                });
            }
        } else if (registrationEmail.value == '') {
            ElNotification({
                title: "注册",
                message: "未输入邮箱",
                type: "warning",
            });
        } else {
            ElNotification({
                title: "注册",
                message: "邮箱格式不对",
                type: "warning",
            });
        }
    } else {
        ElNotification({
            title: "注册",
            message: "未输入账号",
            type: "warning",
        });
    }
    event.preventDefault();
}
// 注册登录转换按钮
function signUp() {
    container.classList.add("right-panel-active")
}
function signIn() {
    container.classList.remove("right-panel-active")
}
</script>

<style lang="less" scoped>
.box {
    margin: 50px auto;
    width: 70vw;
    height: 80vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

h1 {
    font-weight: bold;
    margin: 0;
}

h2 {
    margin-top: 50px;
    text-align: center;
}

p {
    font-size: 14px;
    font-weight: 100;
    line-height: 20px;
    letter-spacing: 0.5px;
    margin: 20px 0 30px;
}

span {
    font-size: 12px;
}

a {
    color: #333;
    font-size: 14px;
    /* 取消下划线 */
    text-decoration: none;
    margin: 15px 0;
}

button {
    border-radius: 20px;
    border: 1px solid #ff416c;
    background: linear-gradient(to right, #ff4b2b, #ff416c);
    color: #ffffff;
    font-size: 12px;
    font-weight: bold;
    padding: 12px 45px;
    letter-spacing: 1px;
    text-transform: uppercase;
    transition: transfrom 80ms ease-in;
}

button:active {
    transform: scale(0.95);
}

button:focus {
    outline: none;
}

button.ghost {
    background-color: transparent;
    border-color: #ffffff;
}

form {
    background-color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    padding: 0 50px;
    height: 100%;
    text-align: center;
}

input {
    background-color: #eee;
    border: none;
    padding: 12px 15px;
    margin: 8px 0;
    width: 100%;
}

.container {
    margin: auto;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
    position: relative;
    overflow: hidden;
    width: 768px;
    max-width: 100%;
    min-height: 480px;
}

.from-container {
    position: absolute;
    top: 0;
    height: 100%;
    transition: all 0.6s ease-in-out;
}

.sign-in-container {
    left: 0;
    width: 50%;
    z-index: 2;
}

.container.right-panel-active .sign-in-container {
    transform: translateX(100%);
}

.sign-up-container {
    left: 0;
    width: 50%;
    opacity: 0;
    z-index: 1;
}

.container.right-panel-active .sign-up-container {
    transform: translateX(100%);
    opacity: 1;
    z-index: 5;
    animation: show 0.6s;
}

@keyframes show {

    0%,
    49.9% {
        opacity: 0;
        z-index: 1;
    }

    50%,
    100% {
        opacity: 1;
        z-index: 5;
    }
}

.overlay-container {
    position: absolute;
    top: 0;
    left: 50%;
    width: 50%;
    height: 100%;
    overflow: hidden;
    transition: transform 0.6s ease-in-out;
    z-index: 100;
}

.container.right-panel-active .overlay-container {
    transform: translateX(-100%);
}

.overlay {
    background: #ff4b2b;
    background: -webkit-linear-gradient(to right, #ff4b2b, #ff416c);
    background: linear-gradient(to right, #ff4b2b, #ff416c);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 0 0;
    color: #ffffff;
    position: relative;
    left: -100%;
    height: 100%;
    width: 200%;
    transform: translateX(0);
    transition: transform 0.6s ease-in-out;
}

.container.right-panel-active .overlay {
    transform: translateX(50%);
}

.overlay-panel {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    text-align: center;
    top: 0;
    height: 100%;
    width: 50%;
    transform: translateX(0);
    transition: transform 0.6s ease-in-out;
}

.overlay-left {
    transform: translateX(-20%);
}

.container.right-panel-active .overlay-left {
    transform: translateX(0);
}

.overlay-right {
    right: 0;
    transform: translateX(0);
}

.container.right-panel-active .overlay-right {
    transform: translateX(20%);
}

.social-container {
    margin: 20px 0;
}

.social-container a {
    border: 1px solid #dddddd;
    border-radius: 50%;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    margin: 0 5px;
    height: 40px;
    width: 40px;
}

footer {
    background-color: #222;
    color: #fff;
    font-size: 14px;
    bottom: 0;
    position: fixed;
    left: 0;
    right: 0;
    text-align: center;
    z-index: 999;
}

footer p {
    margin: 10px 0;
}

footer i {
    color: #ff416c;
}

footer a {
    color: #3c97bf;
    text-decoration: none;
}
</style>